package com.easybuy.service.impl;

import com.easybuy.bean.*;
import com.easybuy.mapper.EasybuyOrderDetailMapper;
import com.easybuy.mapper.EasybuyOrderMapper;
import com.easybuy.service.IOrderService;
import com.easybuy.service.IProductService;
import com.easybuy.service.IUserService;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.sun.tools.internal.ws.processor.model.Request;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.IdGenerator;
import org.springframework.web.bind.annotation.SessionAttribute;
import sun.rmi.rmic.iiop.IDLGenerator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class OrderServiaceImpl implements IOrderService {
    @Autowired
    EasybuyOrderMapper easybuyOrderMapper;
    @Autowired
    IProductService iProductService;
    @Autowired
    IUserService userService;
    @Autowired
    EasybuyOrderDetailMapper easybuyOrderDetailMapper;


    @Override
    public int delete(int id) {
        return easybuyOrderMapper.delete(id);
    }

    @Override
    public int save(EasybuyOrder easybuyOrder) {
        return easybuyOrderMapper.save(easybuyOrder);
    }

    @Override
    public EasybuyOrder find(int eoId) {
        return easybuyOrderMapper.findObject(eoId);
    }

    @Override
    public int updateById(EasybuyOrder order) {
        return easybuyOrderMapper.updata(order);
    }

    @Override
    public Count countRow() {
        return easybuyOrderMapper.count();
    }

    @Override
    public List<EasybuyOrder> listByPage(Integer page, Integer pageSize) {
        int from=(page-1)*pageSize;
        return easybuyOrderMapper.find(from,pageSize);
    }

    @Override
    public List<EasybuyOrder> listByFind(Integer eoId, String eoUserName) {
        EasybuyOrder order=new EasybuyOrder();
        order.setEoId(eoId);
        if(eoUserName==""){
            order.setEoUserName(null);
        }else{
            order.setEoUserName(eoUserName);
        }
        return easybuyOrderMapper.findList(order);
    }


    @Transactional
    public void createOrder(HttpServletRequest request,OrderProductVo orderProductVo) {

        //判断库存
        for(EasybuyProduct easybuyProduct : orderProductVo.getProductList()){
            EasybuyProduct detail=iProductService.detail(easybuyProduct.getEpId());
            if(detail.getEpStock()<easybuyProduct.getCount()){
                throw new RuntimeException("库存不足，请重新下单！");
            }
        }
        //创建订单号
        Integer orderId=new Random().nextInt(1000)+10000;


        BigDecimal totalMoney=BigDecimal.ZERO;
        for (EasybuyProduct easybuyProduct1 :orderProductVo.getProductList()){
            totalMoney.add(easybuyProduct1.getEpPrice().multiply(new BigDecimal(easybuyProduct1.getCount())));

            //创建订单详情
            EasybuyOrderDetail easybuyOrderDetail=new EasybuyOrderDetail();
            easybuyOrderDetail.setEodId(new Random().nextInt(10000)+100000);
            easybuyOrderDetail.setEoId(orderId);
            easybuyOrderDetail.setEpId(easybuyProduct1.getEpId());
            easybuyOrderDetail.setEodQuantity(easybuyProduct1.getCount());
            easybuyOrderDetail.setEodCost(easybuyProduct1.getEpPrice().multiply(new BigDecimal(easybuyProduct1.getCount())));
            easybuyOrderDetailMapper.save(easybuyOrderDetail);
            //减库存
            easybuyProduct1.setEpStock(easybuyProduct1.getEpStock()-easybuyProduct1.getCount());
            iProductService.updateByPk(easybuyProduct1);
        }

        //创建并添加订单
        EasybuyOrder easybuyOrder=new EasybuyOrder();
        easybuyOrder.setEoId(orderId);
        easybuyOrder.setEoCost(totalMoney);
        easybuyOrder.setEoCreateTime(new Date().toString());
        easybuyOrder.setEoStatus(1);

        HttpSession session=request.getSession();
        EasybuyUser user=new EasybuyUser();
        user.setEuUserId(session.getAttribute("username").toString());
        EasybuyUser easybuyUser=userService.selectByPK(user);

        easybuyOrder.setEoUserId(easybuyUser.getEuUserId());
        easybuyOrder.setEoUserAddress(easybuyUser.getEuAddress());
        easybuyOrder.setEoUserName(user.getEuUserName());
        easybuyOrder.setEoType(2);
        easybuyOrderMapper.save(easybuyOrder);




    }


}
